//
//  VisitorLoginView.swift
//  3月5的项目
//
//  Created by 哦哦 on 16/3/6.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
//声明协议 ,需要继承基协议
//OC中有多继承么 如果没有 用什么替代
// optional 还是OC中的关键字 如果在Swift中要使用OC中的一些机制 或者特性 需要在前加入@objc告诉编译器该声明是兼容OC的
@objc  protocol VisitorLoginViewDelegate:NSObjectProtocol{
    //声明代理协议方法   可选的协议方法
    //注销
    optional func userWillRegister()
    //登录
    func userWillLognin()
    
}



class VisitorLoginView: UIView {
    
    var didClickBlock:(()->())?
    
    
    //声明代理对象  代理的声明 在OC中必须使用 weak/assagin  为了防止循环引用
    //声明代理对象(属性)
    weak var visitorDelegate:VisitorLoginViewDelegate?
    
    
    
    //MARK: 外部调用的方法
    func setupInfoWith(title:String,imageName:String?){
        
        tipLabel.text = title
        
        if imageName != nil {
            //不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏大房子
            largeHouse.hidden = true
            //设置不是渐隐
            bringSubviewToFront(circleIcon)
        }else{
            //是首页
            //让圈圈开始转动
            startAnimation()
            
        }
        
        
    }
    private func startAnimation() {
        let anim = CABasicAnimation (keyPath: "transform.rotation")
        //次数 时间 角度
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        anim.toValue = 2 * M_PI
        //默认就是移除动画
        //在动画结束 或者页面处于非活跃状态时 动画效果不移除图层
        anim.removedOnCompletion = false
        
        //添加到图层
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    //要自定义视图 首先找到入口
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 自定义访客视图
    func setUpUI(){
        
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //布局 需要相对于一个已经约束好的视图来添加 链式布局
        circleIcon.snp_makeConstraints { (make) -> Void in
            //            print("jiazai")
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            //            make.centerY.equalTo(circleIcon.snp_centerY).offset(16)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
            
        }
        //按钮约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        //背景颜色
        let value : CGFloat = 236.0 / 255.0;
        
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //添加按钮的点击事件  Selector 无法将一个字符串的引用实例化成为Selector类型的方法选择器
        
        loginBtn.addTarget(self, action: "loginBtndidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtndidClick", forControlEvents: .TouchUpInside)
        
        
    }
    //MARK:  按钮的监听事件
    
    @objc private func loginBtndidClick (){
        print(__FUNCTION__)
        //实现代理方法
        visitorDelegate?.userWillLognin()
        //闭包
//        didClickBlock?()
        
        
    }
    @objc private func registerBtndidClick (){
        print(__FUNCTION__)
        //实现代理方法
        visitorDelegate?.userWillRegister?()
        
    }
    
    
    
    //遮罩图片
    private lazy var maskImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //懒加载子视图
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var largeHouse :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //label
    private lazy var tipLabel : UILabel = UIFactory.labelFactory("", textColor: UIColor.darkGrayColor(), fontSize: 14)
//        
//        let l = UILabel()
//        l.text = "至少你还会吹牛逼，虽然。。。";
//        l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(14);
//        
//        
//        //设置居中
//        l.textAlignment = .Center
//        //设置自动换行
//        l.numberOfLines = 0
//        
//        //自适应大小
//        l.sizeToFit()
//        return l
//    }()
    
    //登录按钮
    private lazy var loginBtn :UIButton = UIFactory.buttonWithtitle("登录", backImageName: "common_button_white_disable", textColor: UIColor.darkGrayColor(), fontSize: 14)
        
//        let btn = UIButton()
//        //这是背景图片
//        //        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        //切图片
//        let image = UIImage (named: "common_button_white_disable")!
//        let capW = Int(image.size.width*0.5)
//        let capH = Int(image.size.height*0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
//        
//        btn.setTitle("登录", forState: .Normal)
//        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
//        
//        btn.sizeToFit()
//        return btn
//    }()
    
    //注册按钮
    private lazy var registerBtn :UIButton = UIFactory.buttonWithtitle("注册", backImageName: "common_button_white_disable", textColor: themeColor, fontSize: 14)
        
        let btn = UIButton()
//        //这是背景图片
//        //        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        let image = UIImage (named: "common_button_white_disable")
//        let capW = Int((image?.size.width)!*0.5)
//        let capH = Int((image?.size.height)!*0.5)
//        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
//
//        
//        btn.setTitle("注册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
//        btn.sizeToFit()
//        return btn
//    }()
    
}
